<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>Python3模块 | 早起的年轻人</title>
    <meta property="og:title" content="Python3模块 - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-09-15T12:51:45&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-09-15T12:51:45&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="Python3模块">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/python_basic/python3%E6%A8%A1%E5%9D%97/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#实例python-30">实例(Python 3.0+)</a></li>
    <li><a href="#import-语句">import 语句</a></li>
    <li><a href="#supportpy-文件代码">support.py 文件代码</a></li>
    <li><a href="#testpy-文件代码">test.py 文件代码</a></li>
    <li><a href="#实例">实例</a></li>
    <li><a href="#实例-1">实例</a></li>
    <li><a href="#from--import-语句">from … import 语句</a></li>
    <li><a href="#from--import--语句">from … import * 语句</a></li>
    <li><a href="#深入模块">深入模块</a></li>
    <li><a href="#__name__属性">__name__属性</a></li>
    <li><a href="#dir-函数">dir() 函数</a></li>
    <li><a href="#标准模块">标准模块</a></li>
    <li><a href="#包">包</a></li>
    <li><a href="#从一个包中导入">从一个包中导入*</a></li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">Python3模块</h1>
        </header>
        <date class="post-meta meta-date">
            2021年9月15日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/python'>python</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <h1 id="python3-模块">Python3 模块</h1>
<p>在前面的几个章节中我们基本上是用 python 解释器来编程，如果你从 Python 解释器退出再进入，那么你定义的所有的方法和变量就都消失了。</p>
<p>为此 Python 提供了一个办法，把这些定义存放在文件中，为一些脚本或者交互式的解释器实例使用，这个文件被称为模块。</p>
<p>模块是一个包含所有你定义的函数和变量的文件，其后缀名是.py。模块可以被别的程序引入，以使用该模块中的函数等功能。这也是使用 python 标准库的方法。</p>
<p>下面是一个使用 python 标准库中模块的例子。</p>
<h2 id="实例python-30">实例(Python 3.0+)</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#!/usr/bin/python3</span>
<span style="color:#75715e"># 文件名: using_sys.py</span>
 
<span style="color:#f92672">import</span> sys
 
<span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;命令行参数如下:&#39;</span>)
<span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> sys<span style="color:#f92672">.</span>argv:
   <span style="color:#66d9ef">print</span>(i)
 
<span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;</span><span style="color:#ae81ff">\n\n</span><span style="color:#e6db74">Python 路径为：&#39;</span>, sys<span style="color:#f92672">.</span>path, <span style="color:#e6db74">&#39;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#39;</span>)
</code></pre></div><p>执行结果如下所示：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#960050;background-color:#1e0010">$</span> python using_sys<span style="color:#f92672">.</span>py <span style="color:#960050;background-color:#1e0010">参数</span><span style="color:#ae81ff">1</span> <span style="color:#960050;background-color:#1e0010">参数</span><span style="color:#ae81ff">2</span>
<span style="color:#960050;background-color:#1e0010">命令行参数如下</span>:
using_sys<span style="color:#f92672">.</span>py
<span style="color:#960050;background-color:#1e0010">参数</span><span style="color:#ae81ff">1</span>
<span style="color:#960050;background-color:#1e0010">参数</span><span style="color:#ae81ff">2</span>


Python <span style="color:#960050;background-color:#1e0010">路径为：</span> [<span style="color:#e6db74">&#39;/root&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4/plat-x86_64-linux-gnu&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4/lib-dynload&#39;</span>, <span style="color:#e6db74">&#39;/usr/local/lib/python3.4/dist-packages&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3/dist-packages&#39;</span>] 
</code></pre></div><ul>
<li>1、import sys 引入 python 标准库中的 sys.py 模块；这是引入某一模块的方法。</li>
<li>2、sys.argv 是一个包含命令行参数的列表。</li>
<li>3、sys.path 包含了一个 Python 解释器自动查找所需模块的路径的列表。</li>
</ul>
<h2 id="import-语句">import 语句</h2>
<p>想使用 Python 源文件，只需在另一个源文件里执行 import 语句，语法如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> module1[, module2[,<span style="color:#f92672">...</span> moduleN]
</code></pre></div><p>当解释器遇到 import 语句，如果模块在当前的搜索路径就会被导入。</p>
<p>搜索路径是一个解释器会先进行搜索的所有目录的列表。如想要导入模块 support，需要把命令放在脚本的顶端：</p>
<h2 id="supportpy-文件代码">support.py 文件代码</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#!/usr/bin/python3</span>
<span style="color:#75715e"># Filename: support.py</span>
 
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">print_func</span>( par ):
    <span style="color:#66d9ef">print</span> (<span style="color:#e6db74">&#34;Hello : &#34;</span>, par)
    <span style="color:#66d9ef">return</span>
</code></pre></div><p>test.py 引入 support 模块：</p>
<h2 id="testpy-文件代码">test.py 文件代码</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#!/usr/bin/python3</span>
<span style="color:#75715e"># Filename: test.py</span>
 
<span style="color:#75715e"># 导入模块</span>
<span style="color:#f92672">import</span> support
 
<span style="color:#75715e"># 现在可以调用模块里包含的函数了</span>
support<span style="color:#f92672">.</span>print_func(<span style="color:#e6db74">&#34;luckly&#34;</span>)
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#960050;background-color:#1e0010">$</span> python3 test<span style="color:#f92672">.</span>py 
Hello :  luckly
</code></pre></div><p>一个模块只会被导入一次，不管你执行了多少次import。这样可以防止导入模块被一遍又一遍地执行。</p>
<p>当我们使用import语句的时候，Python解释器是怎样找到对应的文件的呢？</p>
<p>这就涉及到Python的搜索路径，搜索路径是由一系列目录名组成的，Python解释器就依次从这些目录中去寻找所引入的模块。</p>
<p>这看起来很像环境变量，事实上，也可以通过定义环境变量的方式来确定搜索路径。</p>
<p>搜索路径是在Python编译或安装的时候确定的，安装新的库应该也会修改。搜索路径被存储在sys模块中的path变量，做一个简单的实验，在交互式解释器中，输入以下代码：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">import</span> sys
<span style="color:#f92672">&gt;&gt;&gt;</span> sys<span style="color:#f92672">.</span>path
[<span style="color:#e6db74">&#39;&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4/plat-x86_64-linux-gnu&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3.4/lib-dynload&#39;</span>, <span style="color:#e6db74">&#39;/usr/local/lib/python3.4/dist-packages&#39;</span>, <span style="color:#e6db74">&#39;/usr/lib/python3/dist-packages&#39;</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span> 
</code></pre></div><p>sys.path 输出是一个列表，其中第一项是空串''，代表当前目录（若是从一个脚本中打印出来的话，可以更清楚地看出是哪个目录），亦即我们执行python解释器的目录（对于脚本的话就是运行的脚本所在的目录）。</p>
<p>因此若像我一样在当前目录下存在与要引入模块同名的文件，就会把要引入的模块屏蔽掉。</p>
<p>了解了搜索路径的概念，就可以在脚本中修改sys.path来引入一些不在搜索路径中的模块。</p>
<p>现在，在解释器的当前目录或者 sys.path 中的一个目录里面来创建一个fibo.py的文件，代码如下：</p>
<h2 id="实例">实例</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 斐波那契(fibonacci)数列模块</span>
 
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">fib</span>(n):    <span style="color:#75715e"># 定义到 n 的斐波那契数列</span>
    a, b <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">while</span> b <span style="color:#f92672">&lt;</span> n:
        <span style="color:#66d9ef">print</span>(b, end<span style="color:#f92672">=</span><span style="color:#e6db74">&#39; &#39;</span>)
        a, b <span style="color:#f92672">=</span> b, a<span style="color:#f92672">+</span>b
    <span style="color:#66d9ef">print</span>()
 
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">fib2</span>(n): <span style="color:#75715e"># 返回到 n 的斐波那契数列</span>
    result <span style="color:#f92672">=</span> []
    a, b <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">while</span> b <span style="color:#f92672">&lt;</span> n:
        result<span style="color:#f92672">.</span>append(b)
        a, b <span style="color:#f92672">=</span> b, a<span style="color:#f92672">+</span>b
    <span style="color:#66d9ef">return</span> result
</code></pre></div><p>然后进入Python解释器，使用下面的命令导入这个模块：</p>
<pre><code>&gt;&gt;&gt; import fibo
</code></pre><p>这样做并没有把直接定义在fibo中的函数名称写入到当前符号表里，只是把模块fibo的名字写到了那里。</p>
<p>可以使用模块名称来访问函数：</p>
<h2 id="实例-1">实例</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span>fibo<span style="color:#f92672">.</span>fib(<span style="color:#ae81ff">1000</span>)
<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">5</span> <span style="color:#ae81ff">8</span> <span style="color:#ae81ff">13</span> <span style="color:#ae81ff">21</span> <span style="color:#ae81ff">34</span> <span style="color:#ae81ff">55</span> <span style="color:#ae81ff">89</span> <span style="color:#ae81ff">144</span> <span style="color:#ae81ff">233</span> <span style="color:#ae81ff">377</span> <span style="color:#ae81ff">610</span> <span style="color:#ae81ff">987</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> fibo<span style="color:#f92672">.</span>fib2(<span style="color:#ae81ff">100</span>)
[<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">8</span>, <span style="color:#ae81ff">13</span>, <span style="color:#ae81ff">21</span>, <span style="color:#ae81ff">34</span>, <span style="color:#ae81ff">55</span>, <span style="color:#ae81ff">89</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span> fibo<span style="color:#f92672">.</span>__name__
<span style="color:#e6db74">&#39;fibo&#39;</span>
</code></pre></div><p>如果你打算经常使用一个函数，你可以把它赋给一个本地的名称：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> fib <span style="color:#f92672">=</span> fibo<span style="color:#f92672">.</span>fib
<span style="color:#f92672">&gt;&gt;&gt;</span> fib(<span style="color:#ae81ff">500</span>)
<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">5</span> <span style="color:#ae81ff">8</span> <span style="color:#ae81ff">13</span> <span style="color:#ae81ff">21</span> <span style="color:#ae81ff">34</span> <span style="color:#ae81ff">55</span> <span style="color:#ae81ff">89</span> <span style="color:#ae81ff">144</span> <span style="color:#ae81ff">233</span> <span style="color:#ae81ff">377</span>
</code></pre></div><hr>
<h2 id="from--import-语句">from … import 语句</h2>
<p>Python 的 from 语句让你从模块中导入一个指定的部分到当前命名空间中，语法如下：</p>
<pre><code>from modname import name1[, name2[, ... nameN]]
</code></pre><p>例如，要导入模块 fibo 的 fib 函数，使用如下语句：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">from</span> fibo <span style="color:#f92672">import</span> fib, fib2
<span style="color:#f92672">&gt;&gt;&gt;</span> fib(<span style="color:#ae81ff">500</span>)
<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">5</span> <span style="color:#ae81ff">8</span> <span style="color:#ae81ff">13</span> <span style="color:#ae81ff">21</span> <span style="color:#ae81ff">34</span> <span style="color:#ae81ff">55</span> <span style="color:#ae81ff">89</span> <span style="color:#ae81ff">144</span> <span style="color:#ae81ff">233</span> <span style="color:#ae81ff">377</span>
</code></pre></div><p>这个声明不会把整个fibo模块导入到当前的命名空间中，它只会将fibo里的fib函数引入进来。</p>
<hr>
<h2 id="from--import--语句">from … import * 语句</h2>
<p>把一个模块的所有内容全都导入到当前的命名空间也是可行的，只需使用如下声明：</p>
<pre><code>from modname import *
</code></pre><p>这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。</p>
<hr>
<h2 id="深入模块">深入模块</h2>
<p>模块除了方法定义，还可以包括可执行的代码。这些代码一般用来初始化这个模块。这些代码只有在第一次被导入时才会被执行。</p>
<p>每个模块有各自独立的符号表，在模块内部为所有的函数当作全局符号表来使用。</p>
<p>所以，模块的作者可以放心大胆的在模块内部使用这些全局变量，而不用担心把其他用户的全局变量搞混。</p>
<p>从另一个方面，当你确实知道你在做什么的话，你也可以通过 modname.itemname 这样的表示法来访问模块内的函数。</p>
<p>模块是可以导入其他模块的。在一个模块（或者脚本，或者其他地方）的最前面使用 import 来导入一个模块，当然这只是一个惯例，而不是强制的。被导入的模块的名称将被放入当前操作的模块的符号表中。</p>
<p>还有一种导入的方法，可以使用 import 直接把模块内（函数，变量的）名称导入到当前操作模块。比如:</p>
<pre><code>&gt;&gt;&gt; from fibo import fib, fib2
&gt;&gt;&gt; fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
</code></pre><p>这种导入的方法不会把被导入的模块的名称放在当前的字符表中（所以在这个例子里面，fibo 这个名称是没有定义的）。</p>
<p>这还有一种方法，可以一次性的把模块中的所有（函数，变量）名称都导入到当前模块的字符表:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">from</span> fibo <span style="color:#f92672">import</span> <span style="color:#f92672">*</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> fib(<span style="color:#ae81ff">500</span>)
<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">5</span> <span style="color:#ae81ff">8</span> <span style="color:#ae81ff">13</span> <span style="color:#ae81ff">21</span> <span style="color:#ae81ff">34</span> <span style="color:#ae81ff">55</span> <span style="color:#ae81ff">89</span> <span style="color:#ae81ff">144</span> <span style="color:#ae81ff">233</span> <span style="color:#ae81ff">377</span>
</code></pre></div><p>这将把所有的名字都导入进来，但是那些由单一下划线（_）开头的名字不在此例。大多数情况， Python程序员不使用这种方法，因为引入的其它来源的命名，很可能覆盖了已有的定义。</p>
<hr>
<h2 id="__name__属性">__name__属性</h2>
<p>一个模块被另一个程序第一次引入时，其主程序将运行。如果我们想在模块被引入时，模块中的某一程序块不执行，我们可以用__name__属性来使该程序块仅在该模块自身运行时执行。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#!/usr/bin/python3</span>
<span style="color:#75715e"># Filename: using_name.py</span>

<span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
   <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;程序自身在运行&#39;</span>)
<span style="color:#66d9ef">else</span>:
   <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;我来自另一模块&#39;</span>)
</code></pre></div><p>运行输出如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#960050;background-color:#1e0010">$</span> python using_name<span style="color:#f92672">.</span>py
<span style="color:#960050;background-color:#1e0010">程序自身在运行</span>
<span style="color:#960050;background-color:#1e0010">$</span> python
<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">import</span> using_name
<span style="color:#960050;background-color:#1e0010">我来自另一模块</span>
<span style="color:#f92672">&gt;&gt;&gt;</span>
</code></pre></div><p><strong>说明：</strong> 每个模块都有一个__name__属性，当其值是'<strong>main</strong>&lsquo;时，表明该模块自身在运行，否则是被引入。</p>
<p>说明：<strong><strong>name</strong></strong> 与 <strong><strong>main</strong></strong> 底下是双下划线， **_ _** 是这样去掉中间的那个空格。</p>
<hr>
<h2 id="dir-函数">dir() 函数</h2>
<p>内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">import</span> fibo<span style="color:#f92672">,</span> sys
<span style="color:#f92672">&gt;&gt;&gt;</span> dir(fibo)
[<span style="color:#e6db74">&#39;__name__&#39;</span>, <span style="color:#e6db74">&#39;fib&#39;</span>, <span style="color:#e6db74">&#39;fib2&#39;</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span> dir(sys)  
[<span style="color:#e6db74">&#39;__displayhook__&#39;</span>, <span style="color:#e6db74">&#39;__doc__&#39;</span>, <span style="color:#e6db74">&#39;__excepthook__&#39;</span>, <span style="color:#e6db74">&#39;__loader__&#39;</span>, <span style="color:#e6db74">&#39;__name__&#39;</span>,
 <span style="color:#e6db74">&#39;__package__&#39;</span>, <span style="color:#e6db74">&#39;__stderr__&#39;</span>, <span style="color:#e6db74">&#39;__stdin__&#39;</span>, <span style="color:#e6db74">&#39;__stdout__&#39;</span>,
 <span style="color:#e6db74">&#39;_clear_type_cache&#39;</span>, <span style="color:#e6db74">&#39;_current_frames&#39;</span>, <span style="color:#e6db74">&#39;_debugmallocstats&#39;</span>, <span style="color:#e6db74">&#39;_getframe&#39;</span>,
 <span style="color:#e6db74">&#39;_home&#39;</span>, <span style="color:#e6db74">&#39;_mercurial&#39;</span>, <span style="color:#e6db74">&#39;_xoptions&#39;</span>, <span style="color:#e6db74">&#39;abiflags&#39;</span>, <span style="color:#e6db74">&#39;api_version&#39;</span>, <span style="color:#e6db74">&#39;argv&#39;</span>,
 <span style="color:#e6db74">&#39;base_exec_prefix&#39;</span>, <span style="color:#e6db74">&#39;base_prefix&#39;</span>, <span style="color:#e6db74">&#39;builtin_module_names&#39;</span>, <span style="color:#e6db74">&#39;byteorder&#39;</span>,
 <span style="color:#e6db74">&#39;call_tracing&#39;</span>, <span style="color:#e6db74">&#39;callstats&#39;</span>, <span style="color:#e6db74">&#39;copyright&#39;</span>, <span style="color:#e6db74">&#39;displayhook&#39;</span>,
 <span style="color:#e6db74">&#39;dont_write_bytecode&#39;</span>, <span style="color:#e6db74">&#39;exc_info&#39;</span>, <span style="color:#e6db74">&#39;excepthook&#39;</span>, <span style="color:#e6db74">&#39;exec_prefix&#39;</span>,
 <span style="color:#e6db74">&#39;executable&#39;</span>, <span style="color:#e6db74">&#39;exit&#39;</span>, <span style="color:#e6db74">&#39;flags&#39;</span>, <span style="color:#e6db74">&#39;float_info&#39;</span>, <span style="color:#e6db74">&#39;float_repr_style&#39;</span>,
 <span style="color:#e6db74">&#39;getcheckinterval&#39;</span>, <span style="color:#e6db74">&#39;getdefaultencoding&#39;</span>, <span style="color:#e6db74">&#39;getdlopenflags&#39;</span>,
 <span style="color:#e6db74">&#39;getfilesystemencoding&#39;</span>, <span style="color:#e6db74">&#39;getobjects&#39;</span>, <span style="color:#e6db74">&#39;getprofile&#39;</span>, <span style="color:#e6db74">&#39;getrecursionlimit&#39;</span>,
 <span style="color:#e6db74">&#39;getrefcount&#39;</span>, <span style="color:#e6db74">&#39;getsizeof&#39;</span>, <span style="color:#e6db74">&#39;getswitchinterval&#39;</span>, <span style="color:#e6db74">&#39;gettotalrefcount&#39;</span>,
 <span style="color:#e6db74">&#39;gettrace&#39;</span>, <span style="color:#e6db74">&#39;hash_info&#39;</span>, <span style="color:#e6db74">&#39;hexversion&#39;</span>, <span style="color:#e6db74">&#39;implementation&#39;</span>, <span style="color:#e6db74">&#39;int_info&#39;</span>,
 <span style="color:#e6db74">&#39;intern&#39;</span>, <span style="color:#e6db74">&#39;maxsize&#39;</span>, <span style="color:#e6db74">&#39;maxunicode&#39;</span>, <span style="color:#e6db74">&#39;meta_path&#39;</span>, <span style="color:#e6db74">&#39;modules&#39;</span>, <span style="color:#e6db74">&#39;path&#39;</span>,
 <span style="color:#e6db74">&#39;path_hooks&#39;</span>, <span style="color:#e6db74">&#39;path_importer_cache&#39;</span>, <span style="color:#e6db74">&#39;platform&#39;</span>, <span style="color:#e6db74">&#39;prefix&#39;</span>, <span style="color:#e6db74">&#39;ps1&#39;</span>,
 <span style="color:#e6db74">&#39;setcheckinterval&#39;</span>, <span style="color:#e6db74">&#39;setdlopenflags&#39;</span>, <span style="color:#e6db74">&#39;setprofile&#39;</span>, <span style="color:#e6db74">&#39;setrecursionlimit&#39;</span>,
 <span style="color:#e6db74">&#39;setswitchinterval&#39;</span>, <span style="color:#e6db74">&#39;settrace&#39;</span>, <span style="color:#e6db74">&#39;stderr&#39;</span>, <span style="color:#e6db74">&#39;stdin&#39;</span>, <span style="color:#e6db74">&#39;stdout&#39;</span>,
 <span style="color:#e6db74">&#39;thread_info&#39;</span>, <span style="color:#e6db74">&#39;version&#39;</span>, <span style="color:#e6db74">&#39;version_info&#39;</span>, <span style="color:#e6db74">&#39;warnoptions&#39;</span>]
</code></pre></div><p>如果没有给定参数，那么 dir() 函数会罗列出当前定义的所有名称:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">import</span> fibo
<span style="color:#f92672">&gt;&gt;&gt;</span> fib <span style="color:#f92672">=</span> fibo<span style="color:#f92672">.</span>fib
<span style="color:#f92672">&gt;&gt;&gt;</span> dir() <span style="color:#75715e"># 得到一个当前模块中定义的属性列表</span>
[<span style="color:#e6db74">&#39;__builtins__&#39;</span>, <span style="color:#e6db74">&#39;__name__&#39;</span>, <span style="color:#e6db74">&#39;a&#39;</span>, <span style="color:#e6db74">&#39;fib&#39;</span>, <span style="color:#e6db74">&#39;fibo&#39;</span>, <span style="color:#e6db74">&#39;sys&#39;</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span> a <span style="color:#f92672">=</span> <span style="color:#ae81ff">5</span> <span style="color:#75715e"># 建立一个新的变量 &#39;a&#39;</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> dir()
[<span style="color:#e6db74">&#39;__builtins__&#39;</span>, <span style="color:#e6db74">&#39;__doc__&#39;</span>, <span style="color:#e6db74">&#39;__name__&#39;</span>, <span style="color:#e6db74">&#39;a&#39;</span>, <span style="color:#e6db74">&#39;sys&#39;</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">del</span> a <span style="color:#75715e"># 删除变量名a</span>
<span style="color:#f92672">&gt;&gt;&gt;</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> dir()
[<span style="color:#e6db74">&#39;__builtins__&#39;</span>, <span style="color:#e6db74">&#39;__doc__&#39;</span>, <span style="color:#e6db74">&#39;__name__&#39;</span>, <span style="color:#e6db74">&#39;sys&#39;</span>]
<span style="color:#f92672">&gt;&gt;&gt;</span>
</code></pre></div><h2 id="标准模块">标准模块</h2>
<p>Python 本身带着一些标准的模块库，在 Python 库参考文档中将会介绍到（就是后面的&quot;库参考文档&quot;）。</p>
<p>有些模块直接被构建在解析器里，这些虽然不是一些语言内置的功能，但是他却能很高效的使用，甚至是系统级调用也没问题。</p>
<p>这些组件会根据不同的操作系统进行不同形式的配置，比如 winreg 这个模块就只会提供给 Windows 系统。</p>
<p>应该注意到这有一个特别的模块 sys ，它内置在每一个 Python 解析器中。变量 sys.ps1 和 sys.ps2 定义了主提示符和副提示符所对应的字符串:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#f92672">import</span> sys
<span style="color:#f92672">&gt;&gt;&gt;</span> sys<span style="color:#f92672">.</span>ps1
<span style="color:#e6db74">&#39;&gt;&gt;&gt; &#39;</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> sys<span style="color:#f92672">.</span>ps2
<span style="color:#e6db74">&#39;... &#39;</span>
<span style="color:#f92672">&gt;&gt;&gt;</span> sys<span style="color:#f92672">.</span>ps1 <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;C&gt; &#39;</span>
C<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;luckly!&#39;</span>)
luckly<span style="color:#960050;background-color:#1e0010">!</span>
C<span style="color:#f92672">&gt;</span> 
</code></pre></div><h2 id="包">包</h2>
<p>包是一种管理 Python 模块命名空间的形式，采用&quot;点模块名称&quot;。</p>
<p>比如一个模块的名称是 A.B， 那么他表示一个包 A中的子模块 B 。</p>
<p>就好像使用模块的时候，你不用担心不同模块之间的全局变量相互影响一样，采用点模块名称这种形式也不用担心不同库之间的模块重名的情况。</p>
<p>这样不同的作者都可以提供 NumPy 模块，或者是 Python 图形库。</p>
<p>不妨假设你想设计一套统一处理声音文件和数据的模块（或者称之为一个&quot;包&quot;）。</p>
<p>现存很多种不同的音频文件格式（基本上都是通过后缀名区分的，例如： .wav，:file:.aiff，:file:.au，），所以你需要有一组不断增加的模块，用来在不同的格式之间转换。</p>
<p>并且针对这些音频数据，还有很多不同的操作（比如混音，添加回声，增加均衡器功能，创建人造立体声效果），所以你还需要一组怎么也写不完的模块来处理这些操作。</p>
<p>这里给出了一种可能的包结构（在分层的文件系统中）:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">sound<span style="color:#f92672">/</span>                          <span style="color:#960050;background-color:#1e0010">顶层包</span>
      __init__<span style="color:#f92672">.</span>py               <span style="color:#960050;background-color:#1e0010">初始化</span> sound <span style="color:#960050;background-color:#1e0010">包</span>
      formats<span style="color:#f92672">/</span>                  <span style="color:#960050;background-color:#1e0010">文件格式转换子包</span>
              __init__<span style="color:#f92672">.</span>py
              wavread<span style="color:#f92672">.</span>py
              wavwrite<span style="color:#f92672">.</span>py
              aiffread<span style="color:#f92672">.</span>py
              aiffwrite<span style="color:#f92672">.</span>py
              auread<span style="color:#f92672">.</span>py
              auwrite<span style="color:#f92672">.</span>py
              <span style="color:#f92672">...</span>
      effects<span style="color:#f92672">/</span>                  <span style="color:#960050;background-color:#1e0010">声音效果子包</span>
              __init__<span style="color:#f92672">.</span>py
              echo<span style="color:#f92672">.</span>py
              surround<span style="color:#f92672">.</span>py
              reverse<span style="color:#f92672">.</span>py
              <span style="color:#f92672">...</span>
      filters<span style="color:#f92672">/</span>                  filters <span style="color:#960050;background-color:#1e0010">子包</span>
              __init__<span style="color:#f92672">.</span>py
              equalizer<span style="color:#f92672">.</span>py
              vocoder<span style="color:#f92672">.</span>py
              karaoke<span style="color:#f92672">.</span>py
              <span style="color:#f92672">...</span>
</code></pre></div><p>在导入一个包的时候，Python 会根据 sys.path 中的目录来寻找这个包中包含的子目录。</p>
<p>目录只有包含一个叫做 <strong>init</strong>.py 的文件才会被认作是一个包，主要是为了避免一些滥俗的名字（比如叫做 string）不小心的影响搜索路径中的有效模块。</p>
<p>最简单的情况，放一个空的 :file:<strong>init</strong>.py就可以了。当然这个文件中也可以包含一些初始化代码或者为（将在后面介绍的） __all__变量赋值。</p>
<p>用户可以每次只导入一个包里面的特定模块，比如:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> sound.effects.echo
</code></pre></div><p>这将会导入子模块:sound.effects.echo。 他必须使用全名去访问:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">sound<span style="color:#f92672">.</span>effects<span style="color:#f92672">.</span>echo<span style="color:#f92672">.</span>echofilter(input, output, delay<span style="color:#f92672">=</span><span style="color:#ae81ff">0.7</span>, atten<span style="color:#f92672">=</span><span style="color:#ae81ff">4</span>)
</code></pre></div><p>还有一种导入子模块的方法是:</p>
<pre><code>from sound.effects import echo
</code></pre><p>这同样会导入子模块: echo，并且他不需要那些冗长的前缀，所以他可以这样使用:</p>
<pre><code>echo.echofilter(input, output, delay=0.7, atten=4)
</code></pre><p>还有一种变化就是直接导入一个函数或者变量:</p>
<pre><code>from sound.effects.echo import echofilter
</code></pre><p>同样的，这种方法会导入子模块: echo，并且可以直接使用他的 echofilter() 函数:</p>
<pre><code>echofilter(input, output, delay=0.7, atten=4)
</code></pre><p>注意当使用 <strong>from package import item</strong> 这种形式的时候，对应的 item 既可以是包里面的子模块（子包），或者包里面定义的其他名称，比如函数，类或者变量。</p>
<p>import 语法会首先把 item 当作一个包定义的名称，如果没找到，再试图按照一个模块去导入。如果还没找到，抛出一个 <strong>:exc:ImportError</strong> 异常。</p>
<p>反之，如果使用形如 <strong>import item.subitem.subsubitem</strong> 这种导入形式，除了最后一项，都必须是包，而最后一项则可以是模块或者是包，但是不可以是类，函数或者变量的名字。</p>
<h2 id="从一个包中导入">从一个包中导入*</h2>
<p>如果我们使用 <strong>from sound.effects import *</strong> 会发生什么呢？</p>
<p>Python 会进入文件系统，找到这个包里面所有的子模块，然后一个一个的把它们都导入进来。</p>
<p>但这个方法在 Windows 平台上工作的就不是非常好，因为 Windows 是一个不区分大小写的系统。</p>
<p>在 Windows 平台平台上，我们无法确定一个叫做 ECHO.py 的文件导入为模块是 echo 还是 Echo，或者是 ECHO。</p>
<p>为了解决这个问题，我们只需要提供一个精确包的索引。</p>
<p>导入语句遵循如下规则：如果包定义文件 <strong><strong>init</strong>.py</strong> 存在一个叫做 <strong><strong>all</strong></strong> 的列表变量，那么在使用 <strong>from package import *</strong> 的时候就把这个列表中的所有名字作为包内容导入。</p>
<p>作为包的作者，可别忘了在更新包之后保证 <strong><strong>all</strong></strong> 也更新了啊。</p>
<p>以下实例在 file:sounds/effects/<strong>init</strong>.py 中包含如下代码:</p>
<pre><code>__all__ = [&quot;echo&quot;, &quot;surround&quot;, &quot;reverse&quot;]
</code></pre><p>这表示当你使用from sound.effects import *这种用法时，你只会导入包里面这三个子模块。</p>
<p>如果 <strong><strong>all</strong></strong> 真的没有定义，那么使用**from sound.effects import ***这种语法的时候，就不会导入包 sound.effects 里的任何子模块。他只是把包sound.effects和它里面定义的所有内容导入进来（可能运行__init__.py里定义的初始化代码）。</p>
<p>这会把 <strong>init</strong>.py 里面定义的所有名字导入进来。并且他不会破坏掉我们在这句话之前导入的所有明确指定的模块。看下这部分代码:</p>
<pre><code>import sound.effects.echo
import sound.effects.surround
from sound.effects import *
</code></pre><p>这个例子中，在执行 from&hellip;import 前，包 sound.effects 中的 echo 和 surround 模块都被导入到当前的命名空间中了。（当然如果定义了 <strong>all</strong> 就更没问题了）</p>
<p>通常我们并不主张使用 ***** 这种方法来导入模块，因为这种方法经常会导致代码的可读性降低。不过这样倒的确是可以省去不少敲键的功夫，而且一些模块都设计成了只能通过特定的方法导入。</p>
<p>记住，使用 <strong>from Package import specific_submodule</strong> 这种方法永远不会有错。事实上，这也是推荐的方法。除非是你要导入的子模块有可能和其他包的子模块重名。</p>
<p>如果在结构中包是一个子包（比如这个例子中对于包sound来说），而你又想导入兄弟包（同级别的包）你就得使用导入绝对的路径来导入。比如，如果模块sound.filters.vocoder 要使用包 sound.effects 中的模块 echo，你就要写成 from sound.effects import echo。</p>
<pre><code>from . import echo
from .. import formats
from ..filters import equalizer
</code></pre><p>无论是隐式的还是显式的相对导入都是从当前模块开始的。主模块的名字永远是&quot;<strong>main</strong>&quot;，一个Python应用程序的主模块，应当总是使用绝对路径引用。</p>
<p>包还提供一个额外的属性__path__。这是一个目录列表，里面每一个包含的目录都有为这个包服务的__init__.py，你得在其他__init__.py被执行前定义哦。可以修改这个变量，用来影响包含在包里面的模块和子包。</p>
<p>这个功能并不常用，一般用来扩展包里面的模块。</p>

        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/python_basic/python3%E6%A8%A1%E5%9D%97/">https://luckly.work/post/python_basic/python3%E6%A8%A1%E5%9D%97/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/python_basic/python3%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">Python3数据结构</a></li>
        
        <li><a href="/post/python_basic/python3%E5%87%BD%E6%95%B0/">Python3函数</a></li>
        
        <li><a href="/post/python_basic/python3%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%B8%8E%E7%94%9F%E6%88%90%E5%99%A8/">Python3迭代器与生成器</a></li>
        
        <li><a href="/post/python_basic/python3%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/">Python3循环语句</a></li>
        
        <li><a href="/post/python_basic/python3%E6%9D%A1%E4%BB%B6%E6%8E%A7%E5%88%B6/">Python3条件控制</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/python'>python</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>